package com.newworld.basketballfire.main.utils;

import com.newworld.basketballfire.main.api.DuesType;
import com.newworld.basketballfire.main.api.ErrorCode;
import com.newworld.basketballfire.service.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

/**
 * Created by George on 2019/3/25.
 * InputChecker
 */
public class InputChecker {

    private static Logger logger = LoggerFactory.getLogger(InputChecker.class);

    public static void checkPageSizeAndNo(int pageNo, int PageSize) throws ServiceException {
        if (pageNo <= 0 || PageSize <= 0 || PageSize > 100) {
            logger.error("pageNO or pageSIze is invalid.");
            throw new ServiceException(HttpStatus.BAD_REQUEST, ErrorCode.INVALID_PARAMS, "pageNO or pageSIze is invalid.");
        }
    }

    public static void checkDuesType(int type) throws ServiceException {
        boolean isExist = false;
        for (DuesType value : DuesType.values()) {
            if (type == value.getValue()) {
                isExist = true;
            }
        }

        if (!isExist) {
            logger.error("the dues type is invalid.");
            throw new ServiceException(HttpStatus.BAD_REQUEST, ErrorCode.INVALID_PARAMS, "the dues type is invalid.");
        }
    }

}
